package com.zzzzzz.content.comment;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzzzzz.account.signin.OnlineService;
import com.zzzzzz.account.user.UserBase;
import com.zzzzzz.common.message.ResponseMessage;

@Controller
public class CommentController {
	
	@Resource
	private CommentDao commentDao;
	@Resource
	private OnlineService onlineService;
	
	@RequiresUser
	@RequestMapping(value = "/content/comment/{topicId}", method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> getComments(@PathVariable String topicId, @RequestParam Integer start, @RequestParam Integer offset) throws Exception {
		List<Comment> commentList = commentDao.findByTopicId(topicId, start, offset, true);
		Long count = commentDao.findCountByTopicId(topicId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", commentList);
		map.put("count", count);
		return map;
	}
	
	@RequiresUser
	@RequestMapping(value = "/content/comment/{topicId}", method = RequestMethod.POST)
	public @ResponseBody ResponseMessage addComment(@PathVariable String topicId, @RequestBody CommentForm commentForm) throws Exception {
		
		UserBase userBase = onlineService.findUserBase();
		
		Date now = new Date();
		Comment comment = new Comment();
		comment.setTopicId(topicId);
		comment.setText(commentForm.getText());
		comment.setAddDt(now);
		comment.setUpdDt(now);
		comment.setUserId(userBase.getUserId().toString());
		comment.setUserBase(userBase);
		comment.setStatus(0);
		commentDao.add(comment);
		return new ResponseMessage(ResponseMessage.Type.success, "commentAdded");
	}
	
}
